package com.csi.controller.back;

import cn.hutool.jwt.JWTUtil;
import com.csi.domain.FacilityInfo;
import com.csi.domain.Result;
import com.csi.service.FacilityInfoService;
import com.csi.utils.ReturnResults;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/facilityInfo")
@RestController
public class FacilityInfoController {

    @Autowired
    private FacilityInfoService facilityInfoService;

    /**
     * 新增设施
     * @param facilityInfo
     * @return
     */
    @RequestMapping(value="/save")
    public Object save(@RequestBody FacilityInfo facilityInfo,HttpServletRequest request) {
        String token =  request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        facilityInfo.setHotelId(hotelId);
        int ret = facilityInfoService.save(facilityInfo);

        if (ret > 0) {
            return new Result("新增设施成功！", ReturnResults.ACCESS_SUCCESS);
        }else{
            return new Result("新增设施失败！", ReturnResults.ACCESS_FAILED);
        }
    }

    /**
     * 删除设施
     * @param id
     * @return
     */
    @PostMapping(value="/delete",produces = "application/json;charset=utf-8")
    public Object delete(@RequestParam long id) {
        int ret = facilityInfoService.delete(id);
        if (ret > 0) {
            return new Result("删除设施成功！", ReturnResults.ACCESS_SUCCESS);
        }else{
            return new Result("删除设施失败！", ReturnResults.ACCESS_FAILED);
        }
    }

    /**
     * 修改设施名称
     * @param facilityInfo
     * @return
     */
    @PostMapping(value="/update",produces = "application/json;charset=utf-8")
    public Object update(@RequestBody FacilityInfo facilityInfo) {
        int ret = facilityInfoService.update(facilityInfo);
        if (ret > 0) {
            return new Result("修改成功", ReturnResults.ACCESS_SUCCESS);
        }else{
            return new Result("修改失败", ReturnResults.ACCESS_FAILED);
        }
    }

    /**
     * 查找单个设施
     * @param id
     * @return
     */
    @GetMapping(value="/findById",produces = "application/json;charset=utf-8")
    public Object findById(@RequestParam long id) {
        FacilityInfo facilityInfo = facilityInfoService.findById(id);

        if (facilityInfo != null) {
            return new Result("查找成功", ReturnResults.ACCESS_SUCCESS,facilityInfo);
        }else{
            return new Result("查找失败", ReturnResults.ACCESS_FAILED,facilityInfo);
        }
    }

    /**
     * 查找全部设施信息
     * @return
     */
    @GetMapping(value="/findAll",produces = "application/json;charset=utf-8")
    public Object findAll(HttpServletRequest request) {
        String token =  request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        List<FacilityInfo> facilityInfoList = facilityInfoService.findAll(hotelId);

        if (facilityInfoList != null) {
            return new Result("查找成功", ReturnResults.ACCESS_SUCCESS,facilityInfoList);
        }else{
            return new Result("查找失败", ReturnResults.ACCESS_FAILED,facilityInfoList);
        }
    }

}
